<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" style="width: 100%">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pagesizes.page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="pagesizes.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit? '编辑状态' : '新增状态'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetForm"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getSettingList, getSettingRole, updateSetting, getSettingById, delSettingById } from '@/api/setting'
export default {
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      isEdit: false, // 编辑或者添加
      showDialog: false, // 弹层的显示与隐藏
      list: [], // 保存所有角色列表
      total: 0, // 总条数
      pagesizes: {
        page: 1, // 每页
        pagesize: 5 // 每页条数
      }
    }
  },
  created() {
    this.loadgetSettingList()
  },
  methods: {
    async loadgetSettingList() {
      try {
        const res = await getSettingList(this.pagesizes)
        console.log('getSettingList获取所有角色列表', res)
        if (res.code === 10000) {
          this.list = res.data.rows
          this.total = res.data.total
        }
        // 保存数据
      } catch (err) {
        console.log('getSettingList', err)
      }
    },
    // 每页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesizes.page = 1
      this.pagesizes.pagesize = val
      this.loadgetSettingList()
    },
    // 每条
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pagesizes.page = val
      this.loadgetSettingList()
    },
    // 新增角色
    async doAdd() {
      try {
        const res = await getSettingRole(this.roleForm)
        console.log('getSettingRole新增角色', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 增加让页面调到最后
        this.total++
        const maxTotal = Math.ceil(this.total / this.pagesizes.pagesize)
        this.pagesizes.page = maxTotal
        this.loadgetSettingList()
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('getSettingRole', err)
      }
    },
    // 编辑角色
    async doEdit() {
      try {
        const res = await updateSetting(this.roleForm)
        console.log('updateSetting编辑角色', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadgetSettingList()
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('updateSetting', err)
      }
    },
    // 确认
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 新增角色
    hAdd() {
      this.isEdit = false
      this.showDialog = true
    },
    // 编辑角色
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.loadSettingById(id)
    },
    // 先获取当前数据填充到表单
    async loadSettingById(id) {
      try {
        const res = await getSettingById(id)
        console.log('getSettingById获取角色详情', res)
        if (res.code === 10000) {
          this.roleForm = res.data
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 清空表单
    resetForm() {
      this.$refs.roleForm.resetFields()
    },
    // 删除角色
    hDel(id) {
      this.$confirm('您确认要删除该角色吗').then(async() => {
        const res = await delSettingById(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 删除页面时让最后一页去前一面
        if (this.list.length === 1) {
          this.pagesizes.page--
          if (this.pagesizes.page <= 0) {
            this.pagesizes.page === 1
          }
        }
        this.loadgetSettingList()
      }).then(err => err)
    }
  }
}
</script>

